﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualBasic;

using AdvancedRunAsClasses;
using Impersonation;


namespace AdvancedRunAsConfig
{
    public partial class ConfigForm : Form
    {
        private static RegData cryptoRegistry;
        public ConfigForm()
        {
            InitializeComponent();
            cryptoRegistry = new RegData(); 

        }

        //protected override void Dispose(bool disposing)
        //{
        //  if (disposing)
        //  {
        //    if (components != null)
        //    {
        //      components.Dispose();
        //    }
                         
        //  }

        //  base.Dispose(disposing);
        //}

        /// <summary>
        /// Read key pressed processor 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            int res = cryptoRegistry.ReadRegistry();
            switch (res)
            {
                case 0:
                    toolStripStatusLabel1.Text = "Data read: Success";
                    textBoxUser.Text = cryptoRegistry.User;
                    textBoxDomain.Text = cryptoRegistry.Domain;
                    textBoxPass.Text = cryptoRegistry.Password;
                    textBoxPath.Text = cryptoRegistry.Path;
//                    toolStripStatusLabel1.Text = cryptoRegistry.Password;                               // Temporary
                    break;
                case 1:
                    toolStripStatusLabel1.Text = "Data read: Error - no stored data found";
                    break;
                case 2:
                    textBoxUser.Text = cryptoRegistry.User;
                    textBoxDomain.Text = cryptoRegistry.Domain;
                    textBoxPath.Text = cryptoRegistry.Path;
                    toolStripStatusLabel1.Text = "Data read: Decryption Error - system was changed";
                    break;
                default:
                    toolStripStatusLabel1.Text = "Data read: Unknown error";
                    break;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ConfigForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dlgresult = MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel);
            if (dlgresult == DialogResult.OK)
            {            }
            else 
            {
                e.Cancel = true; 
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBoxUser.Text = textBoxUser.Text.Trim();
            if(textBoxUser.Text.Length == 0)
            {
                toolStripStatusLabel1.Text = "Username can't be null";
                return;
            }

            if (textBoxPass.Text.Length == 0)
            {
                toolStripStatusLabel1.Text = "Password can't be null";
                return;
            }
            
            textBoxDomain.Text = textBoxDomain.Text.Trim();
            textBoxPath.Text = textBoxPath.Text.Trim();
            if (textBoxPath.Text.Length == 0)
            {
                toolStripStatusLabel1.Text = "Path can't be null";
                return;
            }

            cryptoRegistry.User = textBoxUser.Text;
            cryptoRegistry.Domain = textBoxDomain.Text;
            cryptoRegistry.Password = textBoxPass.Text;
            cryptoRegistry.Path = textBoxPath.Text;

            int res = cryptoRegistry.WriteRegistry();
            switch (res)
            {
                case 0:
                    toolStripStatusLabel1.Text = "Data write: Success";
                    break;
                default:
                    toolStripStatusLabel1.Text = "Data write: Unknown error";
                    break;
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = textBoxPath.Text;
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBoxPath.Text = openFileDialog1.FileName;
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 frm = new AboutBox1();
            frm.ShowDialog();
            frm.Dispose();
        }

        private void button4_Click(object sender, EventArgs e)
        {
//            string server = string.Empty;
            
            string server = Microsoft.VisualBasic.Interaction.InputBox("Enter domain controller IP, DNS name or domain name e.g. LDAP://139.53.2.12", "Check credentials", Properties.Settings.Default.DefaultLDAPServer, -1, -1);
            if (server == String.Empty)
            {
                return;
            }
            if(AdvancedRunAsChecks.IsAuthenticated(server, textBoxUser.Text, textBoxPass.Text))
            {
                MessageBox.Show("Provided credentials are correct", "Credentials check", MessageBoxButtons.OK, MessageBoxIcon.Information);
                toolStripStatusLabel1.Text = "Credentials check: OK";
            }
            else
            {
                MessageBox.Show("Provided credentials or server path are incorrect", "Credentials check", MessageBoxButtons.OK, MessageBoxIcon.Error);
                toolStripStatusLabel1.Text = "Credentials check: Error";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string dir = null;
            if(File.Exists(textBoxPath.Text))
            {
                dir = Path.GetDirectoryName(textBoxPath.Text);
            }
            else
            {
                MessageBox.Show("File doesn't exists", "Run Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Impersonator.LaunchNetOnly(textBoxPath.Text, textBoxDomain.Text, textBoxUser.Text, textBoxPass.Text, dir);
        }


    }
}
